<?php

/**
 * Capital Direct Lending (http://www.philbsd.com/cdl)
 *
 * Copyright (C) 2008 Philippine Business Solution Developers <http://www.philbsd.com>
 * All rights reserved.
 *
 *
 * LICENSE
 *
 * This source file is subject to the GNU General Public License v3 license
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-3.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@philbsd.com so we can send you a copy immediately.
 *
 * @copyright  (c) 2008 Philippine Business Solution Developers. (http://www.philbsd.com)
 * @license    http://www.gnu.org/licenses/gpl-3.0.txt
 * @version    $Id: note.php 161 2008-04-13 07:25:10Z artiskool $
 */

if (!defined('BASEPATH')) exit('No direct script access allowed');

class Note extends SB_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->_data['menuLoan'] = true;
        $this->load->model('Loans');
	}

	public function index()
	{
		//$this->input->post();
		//$this->load->library('calendar');
		//echo $this->calendar->generate();
		$this->render('note/index.shtml');
	}

	public function view($id)
	{
		$this->_data['record'] = $this->Loans->getNotes($id);
		$this->render('note/view.shtml');
	}

	public function add()
	{
		$this->render('note/add.shtml');
	}
    public function edit($id)
	{
        $this->_data['record'] = $this->Loans->editNotes($id);
		$this->render('note/edit.shtml');

         // editting notes
         if (isset($_POST['Note/note_description'])) {
            $this->db->trans_begin();
			$data = $this->_swordPost['Note'];
            $this->db->where('id', $id);
            $this->db->update('notes', $data);
            $this->db->trans_commit();
            $this->session->set_flashdata('msg', "Successfully saved!");
            header("Location: {$this->_data['baseUrl']}/note/view/{$_POST['loan_id']}.html");
			exit;
          }
	}

	function popadd($borrower_id)
	{
                $this->_data['borrower_id'] = $borrower_id;
                if ($this->input->post('Note/borrower_id')) {
                        $this->_data['borrower_id'] = $this->input->post('Note/borrower_id');
                }
                $this->load->helper(array('form', 'url'));
                $this->load->library('validation');

                if (isset($_POST['Note/borrower_id'])) {

                        $rules['Note/note_date'] = "required";
                        $rules['Note/note_description'] = "required";
                        $this->validation->set_rules($rules);

                        $fields['Note/note_date'] = 'Note Date';
                        $fields['Note/note_description'] = 'Note';
                        $this->validation->set_fields($fields);

                        if ($this->validation->run()) {
                                $this->db->trans_begin();

                                // adding business
                                $data = $this->_swordPost['Note'];
                                $this->db->insert('notes', $data);
                                $noteId = $this->db->insert_id();

                                if ($this->db->trans_status() === FALSE) {
                                        $this->db->trans_rollback();
                                        $errMsg = "Failed to add!";
                                        if (isset($this->_data['msg'])) {
                                                $this->_data['msg'] .= $errMsg;
                                        } else {
                                                $this->_data['msg'] = $errMsg;
                                        }
                                } else {
                                        $this->db->trans_commit();
                                        $this->_data['success_msg'] = "Successfully saved.";
                                        $this->_data['my_id'] = $noteId;
                                }

                        }
                }

                $this->render('note/popadd.shtml', array(), 'popup.shtml');
	}

	function popdel($note_id)
	{
                $this->_data['note_id'] = $note_id;
                if ($this->input->post('note_id')) {
                        $this->db->delete('notes', array('id' => $this->input->post('note_id')));
                        $this->_data['success_msg'] = "Successfully deleted.";
                }
                $this->render('note/popdel.shtml', array(), 'popup.shtml');
	}
}
